using UnityEngine;
using UnityEngine.UI;

public class PlayerHUDCanvas : PanelsManager
{
	public GUIIStockItemLoader SecondItemLoader;

	public RectTransform Canvas;

	public GameObject SystemHUD;

	public ValueBar HPbar;

	public ValueBar WaterBar;

	public GameObject MiniMap;

	public Minimap Map;

	public Text AmmoText;

	public Image Crosshair;

	public Image CrosshairHit;

	public Image CrosshairScope;

	public Text GameTime;

	public Image ImageDrag;

	public KillFeedPopup KillFeed;

	public Text AliveText;

	public Text CountdownText;

	public Text BroadcastText;

	public GUIItemEquippedLoader Equipped;

	public Text Info;

	private bool isShowinfo;

	private float timeInfo;

	public GameObject ProcessPopup;

	public GameObject MobileController;

	private float broadcastingTime;

	private float broadcastDuration;

	private string broadcastText = string.Empty;

	public void ResetAllHud()
	{
		if ((bool)Equipped)
		{
			Equipped.RestEquipShortcut();
		}
		CloseAllPanels();
	}

	private void Awake()
	{
		UnitZ.Hud = this;
		if (Pages.Length > 0)
		{
			ClosePanel(Pages[0]);
		}
	}

	public Vector2 GetWorldSpaceUIposition(Vector3 position)
	{
		if (Camera.main == null)
		{
			return Vector3.zero;
		}
		Vector2 vector = Camera.main.WorldToViewportPoint(position);
		float x = vector.x;
		Vector2 sizeDelta = Canvas.sizeDelta;
		float num = x * sizeDelta.x;
		Vector2 sizeDelta2 = Canvas.sizeDelta;
		float x2 = num - sizeDelta2.x * 0.5f;
		float y = vector.y;
		Vector2 sizeDelta3 = Canvas.sizeDelta;
		float num2 = y * sizeDelta3.y;
		Vector2 sizeDelta4 = Canvas.sizeDelta;
		Vector2 result = new Vector2(x2, num2 - sizeDelta4.y * 0.5f);
		return result;
	}

	public void ShowInfo(string info, Vector3 position)
	{
		if (Info != null)
		{
			isShowinfo = true;
			Info.gameObject.SetActive(value: true);
			RectTransform component = Info.GetComponent<RectTransform>();
			component.anchoredPosition = GetWorldSpaceUIposition(position);
			Info.text = info;
			timeInfo = Time.time;
		}
	}

	private void Update()
	{
		if (UnitZ.playerManager == null || Canvas == null)
		{
			return;
		}
		bool flag = false;
		if (UnitZ.playerManager.PlayingCharacter == null || ((bool)UnitZ.playerManager.PlayingCharacter && !UnitZ.playerManager.PlayingCharacter.IsAlive))
		{
			Canvas.gameObject.SetActive(value: false);
			if ((bool)Crosshair)
			{
				Crosshair.gameObject.SetActive(value: false);
			}
			if ((bool)CrosshairHit)
			{
				CrosshairHit.gameObject.SetActive(value: false);
			}
			if ((bool)CrosshairScope)
			{
				CrosshairScope.gameObject.SetActive(value: false);
			}
		}
		else
		{
			HumanCharacter component = UnitZ.playerManager.PlayingCharacter.GetComponent<HumanCharacter>();
			if (component != null)
			{
				if ((bool)Equipped)
				{
					Equipped.UpdateEquippedShortcut();
				}
				flag = true;
				Canvas.gameObject.SetActive(value: true);
				if ((bool)Map)
				{
					Map.Player = component.gameObject;
					bool flag2 = false;
					if ((bool)CrosshairScope)
					{
						flag2 = !CrosshairScope.gameObject.activeSelf;
					}
					if (Map.gameObject.activeSelf && !flag2)
					{
						Map.gameObject.SetActive(value: false);
					}
				}
				if ((bool)HPbar)
				{
					HPbar.Value = (int)component.HP;
					HPbar.ValueMax = (int)component.HPmax;
				}
				if ((bool)WaterBar && (bool)component)
				{
					WaterBar.Value = (int)component.Pill;
					WaterBar.ValueMax = (int)component.PillMax;
				}
				if (AmmoText != null && UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment != null)
				{
					AmmoText.text = UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment.Info;
				}
			}
		}
		for (int i = 0; i < Pages.Length; i++)
		{
			if (!Pages[i].LockMouse && Pages[i].gameObject.activeSelf)
			{
				flag = false;
			}
		}
		if (MobileController != null && UnitZ.IsMobile)
		{
			MobileController.gameObject.SetActive(flag);
		}
		if ((bool)MiniMap)
		{
			MiniMap.SetActive(flag);
		}
		MouseLock.MouseLocked = flag;
		if (Info != null)
		{
			Info.gameObject.SetActive(isShowinfo);
			if (Time.time >= timeInfo + 0.1f)
			{
				isShowinfo = false;
			}
		}
		if (BroadcastText != null)
		{
			if (Time.time < broadcastingTime + broadcastDuration)
			{
				BroadcastText.text = broadcastText;
			}
			else
			{
				BroadcastText.text = string.Empty;
			}
		}
	}

	public void OpenSecondInventory(CharacterInventory inventory, string type)
	{
		if (IsPanelOpened("InventoryTrade"))
		{
			ClosePanelByName("InventoryTrade");
			return;
		}
		SecondItemLoader.OpenInventory(inventory, type);
		OpenPanelByName("InventoryTrade");
	}

	public void CloseSecondInventory()
	{
		ClosePanelByName("InventoryTrade");
	}

	public void BroadcastingText(string text, float time)
	{
		broadcastDuration = time;
		broadcastingTime = Time.time;
		broadcastText = text;
	}
}
